package com.eastcode.mobile.client.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eastcode.base.action.BaseAction;
import com.eastcode.mobile.server.system.entity.User;
import com.eastcode.mobile.server.system.service.UserService;
import com.eastcode.mobile.server.system.view.UserView;
import com.eastcode.mobile.server.util.ServerLocator;

public class RegistAction extends BaseAction {

	private static final long serialVersionUID = -9039466736723890137L;
	private UserService userService = ServerLocator.getUserService();

	@Override
	protected void execute() {
		prepare(this);
	}

	/**
	 * 注册
	 * 
	 */
	public void regist() {
		String userId = request.getParameter("userId");
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserView view = parseRequest(UserView.class, "view.");
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", userId);
		view.setParamMap(paramMap);

		List<User> result = userService.searchList(User.class, view);
		if (result != null && result.size() > 0) {
			view.setResult("用户名已经被使用，请重新输入！");
			setResult(view);
		} else {
			User user = new User();
			user.setUserId(userId);
			user.setName(username);
			user.setPassword(password);
			user.setState("1");
			user.setType("2");
			userService.save(user);
			view.setResult("1");
			setResult(view);
		}

		// 跳转到结果页面
		forward("/template/ajax/result.ftl");
	}
}
